package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.GoodsMapper;
import com.mapper.StoreMapper;
import com.pojo.entity.Goods;
import com.pojo.entity.Store;
import com.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 杨世博
 * @date: Created in 11:57 2023/6/9
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {
    @Autowired
    StoreMapper storeMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public IPage<Goods> getGoodsInShop(Integer uid, String key, Integer pageNum, Integer size) {
        QueryWrapper<Store> sqw = new QueryWrapper<>();
        sqw.eq("uid", Long.valueOf(uid));
        Store store = storeMapper.selectOne(sqw);
        IPage<Goods> goodsPage = new Page<>();

        if (store!=null){
            QueryWrapper<Goods> gqw = new QueryWrapper<>();
            List<Goods> goods =  goodsMapper.selectGoodsByStore(store.getMerchantId(), key, (pageNum-1) * size, size);

            goodsPage.setSize(size);
            goodsPage.setRecords(goods);
            goodsPage.setPages(pageNum);

            goodsPage.setCurrent(goodsMapper.selectCountByStore(store.getMerchantId(), key, (pageNum-1) * size, size));
        }else {
            goodsPage = null;
        }


        return goodsPage;
    }
}
